/**
 * 
 */
package com.cqan.account.entity;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.cqan.entity.pk.IdEntity;


/**
 * @Description TODO
 * @author slaton
 * @date 2012-4-27下午6:16:45
 * 
 */
@Entity
//表名与类名不相同时重新定义表名.
@Table(name = "DMS_USER")
//默认的缓存策略.
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@SuppressWarnings("serial")
public class User extends IdEntity implements UserDetails {

	private boolean accountNonExpired;

	private boolean accountNonLocked;

	private boolean activation;

	private String address;

	/**
	 * 权限集合
	 */
	private List<GrantedAuthority> authorities;

	private Date createTime;

	private boolean credentialsNonExpired;

	private List<Department> departments;

	private String email;

	private boolean enabled;

	private Date lastLoginTime;

	private String mobile;

	private String password;

	private String realName;

	private List<Role> roles;

	private String username;

	@Column(name = "ADDRESS")
	public String getAddress() {
		return address;
	}

	@Transient
	public List<GrantedAuthority> getAuthorities() {
		return authorities;
	}

	@Column(name = "CREATE_TIME")
	public Date getCreateTime() {
		return createTime;
	}

	// 多对多定义
	@ManyToMany
	// 中间表定义,表名采用默认命名规则
	@JoinTable(name = "DMS_DEPARTMENT_USER", joinColumns = { @JoinColumn(name = "UID") }, inverseJoinColumns = { @JoinColumn(name = "DID") })
	// Fecth策略定义
	@Fetch(FetchMode.SUBSELECT)
	// 集合按id排序.
	@OrderBy("id")
	// 集合中对象id的缓存.
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Department> getDepartments() {
		return departments;
	}

	@Column(name = "EMAIL")
	public String getEmail() {
		return email;
	}

	@Column(name="LAST_LOGIN_TIME")
	public Date getLastLoginTime() {
		return lastLoginTime;
	}

	@Column(name = "MOBILE")
	public String getMobile() {
		return mobile;
	}

	@Column(name = "PASSWORD")
	public String getPassword() {
		return password;
	}

	@Column(name = "REAL_NAME")
	public String getRealName() {
		return realName;
	}

	// 多对多定义
	@ManyToMany
	// 中间表定义,表名采用默认命名规则
	@JoinTable(name = "DMS_USER_ROLE", joinColumns = { @JoinColumn(name = "UID") }, inverseJoinColumns = { @JoinColumn(name = "RID") })
	// Fecth策略定义
	@Fetch(FetchMode.SUBSELECT)
	// 集合按id排序.
	@OrderBy("id")
	// 集合中对象id的缓存.
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Role> getRoles() {
		return roles;
	}

	@Column(name = "USERNAME")
	public String getUsername() {
		return username;
	}

	@Column(name = "ACCOUNT_NON_EXPIRED")
	public boolean isAccountNonExpired() {
		return accountNonExpired;
	}

	@Column(name = "ACCOUNT_NON_LOCKED")
	public boolean isAccountNonLocked() {
		return accountNonLocked;
	}

	@Column(name = "ACTIVATION")
	public boolean isActivation() {
		return activation;
	}

	@Column(name = "CREDENTIALS_NON_EXPIRED")
	public boolean isCredentialsNonExpired() {
		return credentialsNonExpired;
	}

	@Column(name = "ENABLED")
	public boolean isEnabled() {
		return enabled;
	}

	public void setAccountNonExpired(boolean accountNonExpired) {
		this.accountNonExpired = accountNonExpired;
	}

	public void setAccountNonLocked(boolean accountNonLocked) {
		this.accountNonLocked = accountNonLocked;
	}

	public void setActivation(boolean activation) {
		this.activation = activation;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void setAuthorities(List<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public void setCredentialsNonExpired(boolean credentialsNonExpired) {
		this.credentialsNonExpired = credentialsNonExpired;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public void setUsername(String username) {
		this.username = username;
	}

}
